<?php


	class FTAProducts extends FTAObject {
		
		var $cmsDocument;
		var $allProductCategories;
		var $productCategory;
		var $masterProducts;
		var $masterProduct;
		var $productVariants;
		var $productVariant;
		
		var $masterImageElementId;
		var $otherImageElementId;
		
		var $enlargedImageFiletypecode;
		var $thumbImageFiletypecode;
		var $smallThumbImageFiletypecode;
		
		// depending on which field holds the dropdown for product gallery in master product module
		// this can be changed if for a site gallery is in a different field
		var $prodGalleryFieldInMasterProductModule;
		
		var $pageSize;
		var $totalPages;
		
		
		
		
		function FTAProducts() {
			//parent::FTAObject();
			parent::__construct();
			
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			
			// added this for setting pageNumber.. since flush() doesn't flush the page number...
			$this->cmsDocument2 = new CmsDocument($db);
			//set up the base options on the cms document.. so we only need to do it once??
			global $FTAProductMasterImageElementId;			
			$this->masterImageElementId = (0 < $FTAProductMasterImageElementId) ? $FTAProductMasterImageElementId : 10104;
			
			global $FTAProductOtherImageElementId;			
			$this->otherImageElementId = (0 < $FTAProductOtherImageElementId) ? $FTAProductOtherImageElementId : 11362;

			global $FTAProductEnlargedImageFiletypecode;
			$this->enlargedImageFiletypecode = (0 < $FTAProductEnlargedImageFiletypecode) ? $FTAProductEnlargedImageFiletypecode : 3;
			
			global $FTAProductThumbImageFiletypecode;
			$this->thumbImageFiletypecode = (0 < $FTAProductThumbImageFiletypecode) ? $FTAProductThumbImageFiletypecode : 2;
			
			global $FTAProductSmallerThumbImageFiletypecode;
			$this->smallThumbImageFiletypecode = (0 < $FTAProductSmallerThumbImageFiletypecode) ? $FTAProductSmallerThumbImageFiletypecode : 4;
			
			global $FTAProductGalleryFieldInMasterProductModule;
			$this->prodGalleryFieldInMasterProductModule = (0 < strlen($FTAProductGalleryFieldInMasterProductModule)) ? $FTAProductGalleryFieldInMasterProductModule : "extra15";
			
			global $FTAProductPageSizeForMasterProductsListPage;
			$this->pageSize = (0 < $FTAProductPageSizeForMasterProductsListPage) ? $FTAProductPageSizeForMasterProductsListPage : 1;			
		}
		
		function getProductCategories(){
			// if (is_null($this->allProductCategories)){
			
				$this->cmsDocument->flush();
				$this->cmsDocument->setActiveDocuments();
				$this->cmsDocument->addDocumentType('docProdCat');
				$this->cmsDocument->addSort('<<md.rank>>');
				$this->cmsDocument->addSort('title');
				$this->cmsDocument->setPageSize(null);
				$this->cmsDocument->setPageNumber(null);
				$this->cmsDocument->resetIterator();
			    
			    
			   // $this->allProductCategories = array();
			   //	while ($row = $this->cmsDocument->nextDocumentFromCache()){
			   //	array_push($this->allProductCategories,$row);
			   //	}
				
				$this->allProductCategories = $this->cmsDocument->getAllDocuments();
				// echo $this->cmsDocument->countQuery();
				if (!is_array($this->allProductCategories))
					$this->allProductCategories = array();

			// }
			
			return $this->allProductCategories;
		}
		
		function getMasterProducts($categoryIdentifier = NULL){
				$this->productCategory = NULL;
				
				$page = extractNumberWithKeyFromArray("page",$_REQUEST,null);
				
				// get document code of the product category
				if(!(is_null($categoryIdentifier) || $categoryIdentifier == "")){
					$this->cmsDocument->flush();
					$this->cmsDocument->setActiveDocuments();
					$this->cmsDocument->addDocumentType('docProdCat');
					$this->cmsDocument->addConstraint('title', $categoryIdentifier);
					$this->cmsDocument->addSort('<<md.rank>>');
					$this->cmsDocument->addSort('title');
					$this->cmsDocument->setPageSize(null);
					$this->cmsDocument->resetIterator();
					
					$this->productCategory = $this->cmsDocument->openCurrentDocumentFromCache();
				}
				
				// get master products
				$this->cmsDocument2->flush();
				$this->cmsDocument2->setActiveDocuments();
				$this->cmsDocument2->addDocumentType('docMasterPro');
				
				// if there is valid category, get master products for this product category
				if($this->productCategory){
					$this->cmsDocument2->addConstraint('categorycode', $this->productCategory->documentcode);
				}
								
				$this->cmsDocument2->addSort('<<md.rank>>');
				$this->cmsDocument2->addSort('title');
				$this->cmsDocument2->setPageNumber($page);			
				$this->cmsDocument2->setPageSize($this->pageSize);
				$this->cmsDocument2->resetIterator();
				
				$this->totalPages = $this->cmsDocument2->totalPages();
				// echo $this->cmsDocument->countQuery();
				$this->masterProducts = $this->cmsDocument2->getAllDocuments();
				
				if (!is_array($this->masterProducts))
					$this->masterProducts = array();
			
				return $this->masterProducts;
		}
		
		function getMasterProduct($categoryIdentifier, $masterProductIdentifier){
			
			$this->productCategory = NULL;
			$this->masterProduct = NULL;
			
			if(!(is_null($categoryIdentifier) || $categoryIdentifier == "")){
					$this->cmsDocument->flush();
					$this->cmsDocument->setActiveDocuments();
					$this->cmsDocument->addDocumentType('docProdCat');
					$this->cmsDocument->addConstraint('title', $categoryIdentifier);
					$this->cmsDocument->addSort('<<md.rank>>');
					// $this->cmsDocument->addSort('title');
					$this->cmsDocument->setPageSize(1);
					$this->cmsDocument->resetIterator();
					
					$this->productCategory = $this->cmsDocument->openCurrentDocumentFromCache();
			}
			
			
			if($this->productCategory && !(is_null($masterProductIdentifier) || $masterProductIdentifier == "")){
				
				$this->cmsDocument->flush();
				$this->cmsDocument->setActiveDocuments();
				$this->cmsDocument->addDocumentType('docMasterPro');
				
				if (is_numeric($masterProductIdentifier))
					$this->cmsDocument->addConstraint('documentcode', $masterProductIdentifier);
				else 
					$this->cmsDocument->addConstraint('title', $masterProductIdentifier);
					
				if($this->productCategory){
					$this->cmsDocument->addConstraint('categorycode', $this->productCategory->documentcode);
				}
				
				$this->cmsDocument->addSort('<<md.rank>>');
				$this->cmsDocument->addSort('title');
				$this->cmsDocument->setPageSize(1);
				$this->cmsDocument->resetIterator();
				
				$this->masterProduct = $this->cmsDocument->openCurrentDocumentFromCache();
				
			}
			
			if (!is_null($this->masterProduct)) {
				return true;
			} else {
				return false;
			}
		}
		
		// need to call getMasterProduct($c,$m) first and then this:
		function getProductVariants(){
				if(!($this->masterProduct))
					return;	
				
				// get product variants:
				$this->cmsDocument->flush();
				$this->cmsDocument->setActiveDocuments();
				$this->cmsDocument->addDocumentType('docProduct');
				$this->cmsDocument->addConstraint('categorycode', $this->masterProduct->documentcode);
				$this->cmsDocument->addSort('<<md.rank>>');
				$this->cmsDocument->addSort('title');
				$this->cmsDocument->setPageSize(null);
				$this->cmsDocument->resetIterator();
				
				$this->productVariants = $this->cmsDocument->getAllDocuments();
				
				if (!is_array($this->productVariants))
					$this->productVariants = array();
			
			return $this->productVariants;
		}
		
		// identifier must be a documentcode
		function getProductVariant($documentIdentifier){
				// get product variant:
				$this->cmsDocument->flush();
				$this->cmsDocument->setActiveDocuments();
				$this->cmsDocument->addDocumentType('docProduct');
				$this->cmsDocument->addConstraint('documentcode', $documentIdentifier);
				$this->cmsDocument->addSort('<<md.rank>>');
				$this->cmsDocument->addSort('title');
				$this->cmsDocument->setPageSize(1);
				$this->cmsDocument->resetIterator();
				
				$this->productVariant = $this->cmsDocument->openCurrentDocumentFromCache();
				
				if (!$this->productVariant)
					$this->productVariant = null;
			
			return $this->productVariant;
		}


		function renderProductCategoryMenu(){
			if(!is_array($this->allProductCategories))
				return;	
?>
			<ul class="productsList">
<?
			foreach($this->allProductCategories as $productCat){
				$productCatURL = "/Products/" . urlFragmentClean($productCat->title) . "/";
?>
                    <li><a href="<?= $productCatURL; ?>"><?= $productCat->title; ?></a></li>  
<?
			}
?>
            </ul>
<?
		}
		
		function renderAllMasterProducts(){
			if(!is_array($this->masterProducts))
				return;	
?>
				<ul class="productList">
<?
				foreach($this->masterProducts as $masterPro){
					if(!is_null($this->productCategory)){
						$prodCat = $this->productCategory->title;
					} else {
						$prodCat = $this->cmsDocument->openDocumentFromCache($masterPro->categorycode);
						$prodCat = $prodCat->title;
					}
					
					$masterProURL = "/Products/" . urlFragmentClean($prodCat) . "/" . urlFragmentClean($masterPro->title) . "/";
					
					
					$fieldForGalleryInMasterProdModule = $this->prodGalleryFieldInMasterProductModule;	
					$productGalleryId = $masterPro->$fieldForGalleryInMasterProdModule;
					
					$this->cmsDocument->flush();
					$this->cmsDocument->setActiveDocuments();
					$this->cmsDocument->addDocumentType('docFileMod');
					$this->cmsDocument->addConstraint('author',1);			
					$this->cmsDocument->addConstraint('documentcode',$productGalleryId);
					$this->cmsDocument->addSort('<<rank>>');				
					$this->cmsDocument->setPageSize(1);				
					$this->cmsDocument->resetIterator();
					
					$masterProImg ="/images/productList_thumb.jpg";
					if($this->cmsDocument->totalDocumentsOnPage() == 1){
						$productGalleryMainFolder = $this->cmsDocument->openCurrentDocumentFromCache();
						
						$this->cmsDocument->flush();
						$this->cmsDocument->setActiveDocuments();
						$this->cmsDocument->addDocumentType('docFileMod');
						$this->cmsDocument->addConstraint('author',0);			
						$this->cmsDocument->addConstraint('categorycode',$productGalleryMainFolder->documentcode);
						$this->cmsDocument->addSort('<<rank>>');				
						$this->cmsDocument->setPageSize(1);				
						$this->cmsDocument->resetIterator();
						
						if($this->cmsDocument->totalDocumentsOnPage() == 1){
							$image = $this->cmsDocument->openCurrentDocumentFromCache();
							$masterProImg = "/html/blob.php?documentCode=" . $image->documentcode . "&filetypecode=" . $this->thumbImageFiletypecode . "&elementId=" . $this->otherImageElementId;
						} 
						
					}
?>				
			        <li><a href="<?= $masterProURL; ?>" title="<?= $masterPro->title; ?>"><img src="<?= $masterProImg; ?>" alt="<?= $masterPro->title; ?>" /></a><br />
			            <a href="<?= $masterProURL; ?>" title="<?= $masterPro->title; ?>"><?= $masterPro->title; ?></a><br />
			            From $<?= $masterPro->value; ?>
			        </li>
<?
				}
?>
				</ul>
<?
		}
		
		function renderProductCategoryTitle(){
			if(is_null($this->productCategory)){
				return;
			} else {	
				return $this->productCategory->title;
			}				
		}
		
		function renderMasterProductTitle(){
			if(is_null($this->masterProduct)){
				return;
			} else {	
				return $this->masterProduct->title;
			}				
		}
		
		function renderMasterProductDescription(){
			if(is_null($this->masterProduct)){
				return;
			} else {	
				return $this->masterProduct->shortdescription;
			}				
		}
		
		function renderMasterProductPrice(){
			if(is_null($this->masterProduct)){
				return;
			} else {	
				return $this->masterProduct->value;
			}				
		}
		
		function renderTotalPages(){
			if(!is_array($this->masterProducts)){
				return;
			} else {
				return $this->totalPages;
			}
		}
		

		function renderMasterProductImage(){
			if(is_null($this->masterProduct)){
				return;
			} else {	
				$masterProImg = "/html/blob.php?documentCode=" . $this->masterProduct->documentcode . "&filetypecode=" . $this->enlargedImageFiletypecode . "&elementId=" . $this->masterImageElementId;
?>
				<img src="<?= $masterProImg; ?>" alt="<?= $this->masterProduct->title; ?>" />
<?
			}				
		}
		
		// should be under custom code, but this can be used in other websites as well :)
		function renderOtherProductImages(){
			
			if(is_null($this->masterProduct))
				return;
				
			$fieldForGalleryInMasterProdModule = $this->prodGalleryFieldInMasterProductModule;	
			$productGalleryId = $this->masterProduct->$fieldForGalleryInMasterProdModule;
			
			if(is_null($productGalleryId) || $productGalleryId == ""){
				return;
			} else {
				
				$this->cmsDocument->flush();
				$this->cmsDocument->setActiveDocuments();
				$this->cmsDocument->addDocumentType('docFileMod');
				$this->cmsDocument->addConstraint('author',1);			
				$this->cmsDocument->addConstraint('documentcode',$productGalleryId);
				$this->cmsDocument->addSort('<<rank>>');				
				$this->cmsDocument->setPageSize(1);				
				$this->cmsDocument->resetIterator();
				
				if($this->cmsDocument->totalDocumentsOnPage() == 1){
					
					$productGalleryMainFolder = $this->cmsDocument->openCurrentDocumentFromCache();
					
					$this->cmsDocument->flush();
					$this->cmsDocument->setActiveDocuments();
					$this->cmsDocument->addDocumentType('docFileMod');
					$this->cmsDocument->addConstraint('author',0);			
					$this->cmsDocument->addConstraint('categorycode',$productGalleryMainFolder->documentcode);
					$this->cmsDocument->addSort('<<rank>>');				
					$this->cmsDocument->setPageSize(null);				
					$this->cmsDocument->resetIterator();
					
					if($this->cmsDocument->totalDocuments() > 0){
						
						$allImages = $this->cmsDocument->getAllDocuments();
?>


<ul class="productVariants">
<?
						foreach($allImages as $image){
							$imageSrc =  "/html/blob.php?documentCode=" . $image->documentcode . "&filetypecode=" . $this->smallThumbImageFiletypecode . "&elementId=" . $this->otherImageElementId;
?>
			                    <li><a rel="imageMe"><img id="im_<?= $image->documentcode ?>" src="<?= $imageSrc; ?>" alt="<?= $image->title; ?>" class="img"/></a></li>							
<?
						}
?>
</ul> 

<?
					} else {
						return;
					}
					
				} else {
					return;
				}								
			}							
		}
		
		function getFirstImageObjectInProductGallery(){
//
			if(is_null($this->masterProduct))
				return null;
				
			$fieldForGalleryInMasterProdModule = $this->prodGalleryFieldInMasterProductModule;	
			$productGalleryId = $this->masterProduct->$fieldForGalleryInMasterProdModule;
			
			if(is_null($productGalleryId) || $productGalleryId == ""){
				return null;
			} else {
				$this->cmsDocument->flush();
				$this->cmsDocument->setActiveDocuments();
				$this->cmsDocument->addDocumentType('docFileMod');
				$this->cmsDocument->addConstraint('author',1);			
				$this->cmsDocument->addConstraint('documentcode',$productGalleryId);
				$this->cmsDocument->addSort('<<rank>>');				
				$this->cmsDocument->setPageSize(1);				
				$this->cmsDocument->resetIterator();
				
				if($this->cmsDocument->totalDocumentsOnPage() == 1){
					$productGalleryMainFolder = $this->cmsDocument->openCurrentDocumentFromCache();
					
					$this->cmsDocument->flush();
					$this->cmsDocument->setActiveDocuments();
					$this->cmsDocument->addDocumentType('docFileMod');
					$this->cmsDocument->addConstraint('author',0);			
					$this->cmsDocument->addConstraint('categorycode',$productGalleryMainFolder->documentcode);
					$this->cmsDocument->addSort('<<rank>>');
					$this->cmsDocument->addSort('documentcode');				
					$this->cmsDocument->setPageSize(1);				
					$this->cmsDocument->resetIterator();
					
					if($this->cmsDocument->totalDocumentsOnPage() == 1){
						return $this->cmsDocument->openCurrentDocumentFromCache();						
					} else {
						return null;
					}
					
				} else {
					return null;
				}								
			}	
			return null;

//			
		}
		
		function renderFirstImageOfProductGallery(){
			$firstImage = $this->getFirstImageObjectInProductGallery();
			if ($firstImage) {
				$imageSrc =  "/html/blob.php?documentCode=" . $firstImage->documentcode . "&filetypecode=" . $this->enlargedImageFiletypecode . "&elementId=" . $this->otherImageElementId;
				
?>		
	<img id="displayImage" src="<?= $imageSrc; ?>" alt="<?= $firstImage->title; ?>" />
<?php 		    
			}                						
		}
		
		function renderProductVariantsAsDropdown(){
			if(!is_array($this->productVariants)){
				return;
			} else {
?>
			<select name="variant">
<?
				foreach($this->productVariants as $prodVariant){
?>
	                	  <option value="<?= $prodVariant->documentcode; ?>"><?= $prodVariant->title; ?></option>	            
<?
				}
?>
			 </select>
<?		
			}				
		}	
		
		function renderListOfOtherProductsInTheCategory(){
			if(!is_array($this->masterProducts))
				return;
				
				$categoryFromRequest = extractStringWithKeyFromArray('prodCat',$_REQUEST,null);
				$categoryFromRequest = urlFragmentCleanRestore($categoryFromRequest);
				
				$masterProductFromRequest = extractStringWithKeyFromArray('masterPro',$_REQUEST,null);
				$masterProductFromRequest = urlFragmentCleanRestore($masterProductFromRequest);
				
				$otherMasterProductsInThisCat = array();
				foreach($this->masterProducts as $masterOtherInThisCat){
					if($masterOtherInThisCat->title != $masterProductFromRequest){
						array_push($otherMasterProductsInThisCat, $masterOtherInThisCat);
					} 
				}
				
				if(count($otherMasterProductsInThisCat) > 0){					
?>
				<ul class="productsList">
<?				
				foreach($otherMasterProductsInThisCat as $masterOther){
					$masterProURL = "/Products/" . urlFragmentClean($categoryFromRequest) . "/" . urlFragmentClean($masterOther->title) . "/";
?>					
                    	<li><a href="<?= $masterProURL; ?>"><?= $masterOther->title; ?></a></li>            
<?					
				}
?>
				</ul>
<?				} else {
					return;
				}				
		}

		function renderProductVariant(){
			if(is_null($this->productVariant)){
				return;
			} else {	
				return $this->productVariant;
			}				
		}
			
	}

	// get menu of categories
	function fta_getProductsCategoryMenu() {
		ob_start();
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getProductCategories();
		$productsObject->renderProductCategoryMenu();
		return ob_get_clean();			
	}
	
	// get all master products
	function fta_getAllMasterProducts($categoryIdentifier) {
		ob_start();
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProducts($categoryIdentifier);
		$productsObject->renderAllMasterProducts();
		return ob_get_clean();			
	}
	
	// get Product Category title
	function fta_getProductCategoryTitle($categoryIdentifier, $masterProductIdentifier) {
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProduct($categoryIdentifier, $masterProductIdentifier);
		return $productsObject->renderProductCategoryTitle();		
	}
	
	// get master product title
	function fta_getMasterProductTitle($categoryIdentifier, $masterProductIdentifier) {
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProduct($categoryIdentifier, $masterProductIdentifier);
		return $productsObject->renderMasterProductTitle();		
	}
	
	// get master product description
	function fta_getMasterProductDescription($categoryIdentifier, $masterProductIdentifier) {
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProduct($categoryIdentifier, $masterProductIdentifier);
		return $productsObject->renderMasterProductDescription();		
	}
	
	// get master product price
	function fta_getMasterProductPrice($categoryIdentifier, $masterProductIdentifier) {
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProduct($categoryIdentifier, $masterProductIdentifier);
		return $productsObject->renderMasterProductPrice();		
	}
	
	function fta_getTotalPagesforMasterProductList($categoryIdentifier = NULL) {
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProducts($categoryIdentifier);
		return $productsObject->renderTotalPages();		
	}
	
	// if there's a Master Product Image then use this function
	function fta_getMasterProductImage($categoryIdentifier, $masterProductIdentifier) {
		ob_start();
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProduct($categoryIdentifier, $masterProductIdentifier);
		$productsObject->renderMasterProductImage();
		return ob_get_clean();		
	}
	
	// if there's an image gallery for Master Product, then use this function
	function fta_getFirstImageOfProductGallery($categoryIdentifier, $masterProductIdentifier) {
		ob_start();
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProduct($categoryIdentifier, $masterProductIdentifier);
		$productsObject->renderFirstImageOfProductGallery();
		return ob_get_clean();		
	}
	
	
	// get other product image thumbnails
	function fta_getOtherProductImageThumbs($categoryIdentifier, $masterProductIdentifier) {
		ob_start();
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProduct($categoryIdentifier, $masterProductIdentifier);
		$productsObject->renderOtherProductImages();
		return ob_get_clean();		
	}
	
	// get product variants as a dropdown for the specified master product
	function fta_getProductVariantAsDropdown($categoryIdentifier, $masterProductIdentifier) {
		ob_start();
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProduct($categoryIdentifier, $masterProductIdentifier);
		$productsObject->getProductVariants();
		$productsObject->renderProductVariantsAsDropdown();
		return ob_get_clean();			
	}
	
	function fta_getProductVariant($documentIdentifier) {
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getProductVariant();
		$productsObject->renderProductVariant();		
	}
	
	// get a list of other products (master) for the specified product category
	function fta_getListOfOtherProductsInTheCategory($categoryIdentifier) {
		ob_start();
		$productsObject = fta_getSharedProductsObject();		
		$productsObject->getMasterProducts($categoryIdentifier);
		$productsObject->renderListOfOtherProductsInTheCategory();
		return ob_get_clean();			
	}
	
	
	
	
	
	/**
	 * Gets the fta obejct.. these are singletons really..
	 *
	 * @return FTAWebPage
	 */
	function fta_getSharedProductsObject(){

		global $fta_;

		global $FTAProductsClass;
		
		if(!is_null($FTAProductsClass) && "FTAProducts" != $FTAProductsClass){
		} else {
			$FTAProductsClass = "FTAProducts";
		}
			
		if (!array_key_exists('ftaProducts',$fta_)){
			$fta_->ftaProducts = new  $FTAProductsClass();
		}
		return $fta_->ftaProducts;
	}